package com.nearby.demand.ui;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.EditText;
import android.widget.ImageView;

import com.nearby.demand.R;
import com.nearby.demand.utils.VersionUtil;
import com.nearby.demand.utils.WindowUtil;
import com.nineoldandroids.animation.ObjectAnimator;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.codetail.animation.SupportAnimator;
import io.codetail.animation.arcanimator.ArcAnimator;
import io.codetail.animation.arcanimator.Side;

/**
 * Created by Administrator on 2016/home_2/25.
 */
public class TestAD extends AppCompatActivity implements View.OnClickListener {
    @Bind(R.id.card_search)
    CardView mCardView;
    @Bind(R.id.edit_text_search)
    EditText edit_text_search;
    @Bind(R.id.iv_bottom_search)
    ImageView iv_bottom_search;
    @Bind(R.id.view_hide)
    View view_hide;
    @Bind(R.id.fab)
    FloatingActionButton fab;
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_ad);
        ButterKnife.bind(this);
        fab.setOnClickListener(this);
        view_hide.setOnClickListener(this);
        handFabPathAndSearch();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fab:
                view_hide.setVisibility(View.VISIBLE);
                ArcAnimator.createArcAnimator(fab, WindowUtil.getScreenWidth() / 2, WindowUtil.getStatusHeight() + (toolbar.getHeight() / 2), 45.0f, Side.LEFT)
                        .setDuration(500)
                        .start();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mCardView.setVisibility(View.VISIBLE);
                        fab.setVisibility(View.GONE);
                        iv_bottom_search.performClick();
                    }
                }, 600);

                break;
            case R.id.view_hide:
                edit_text_search.setText("");
                iv_bottom_search.performClick();
                break;
        }
    }

    private SupportAnimator mAnimator;
    private void handFabPathAndSearch() {
        iv_bottom_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAnimator != null && !mAnimator.isRunning()) {
                    mAnimator = mAnimator.reverse();
                    float curTranslationX = iv_bottom_search.getTranslationX();
                    final ObjectAnimator animator = ObjectAnimator.ofFloat(iv_bottom_search, "translationX", curTranslationX, 0);
                    animator.setDuration(600);
                    mAnimator.addListener(new SupportAnimator.AnimatorListener() {
                        @Override
                        public void onAnimationStart() {
                            animator.start();
                        }

                        @Override
                        public void onAnimationEnd() {
                            mAnimator = null;
                            fab.setVisibility(View.VISIBLE);
                            mCardView.setVisibility(View.GONE);
                            if (VersionUtil.checkVersionIntMoreThan19()) {
                                ArcAnimator.createArcAnimator(fab, WindowUtil.getScreenWidth() - fab.getWidth() / 2 - WindowUtil.dip2px(16), WindowUtil.getScreenHeight() - fab.getHeight() - WindowUtil.dip2px(16), 45.0f, Side.LEFT)
                                        .setDuration(500)
                                        .start();
                            } else {
                                ArcAnimator.createArcAnimator(fab, WindowUtil.getScreenWidth() - fab.getWidth() / 2 - WindowUtil.dip2px(16), WindowUtil.getScreenHeight() - fab.getHeight() / 2 - WindowUtil.dip2px(16), 45.0f, Side.LEFT)
                                        .setDuration(500)
                                        .start();
                            }
                            view_hide.setVisibility(View.GONE);
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                }
                            }, 500);

                        }

                        @Override
                        public void onAnimationCancel() {

                        }

                        @Override
                        public void onAnimationRepeat() {

                        }
                    });
                } else if (mAnimator != null) {
                    mAnimator.cancel();
                    return;
                } else {
                    int cx = mCardView.getRight();
                    int cy = mCardView.getBottom();
                    float curTranslationX = iv_bottom_search.getTranslationX();
                    final ObjectAnimator animator = ObjectAnimator.ofFloat(iv_bottom_search, "translationX", curTranslationX, cx / 2 - WindowUtil.dip2px(24));
                    animator.setDuration(600);
                    float radius = r(mCardView.getWidth(), mCardView.getHeight());
                    mAnimator = io.codetail.animation.ViewAnimationUtils.createCircularReveal(mCardView, cx / 2, cy - WindowUtil.dip2px(32), WindowUtil.dip2px(20), radius);
                    mAnimator.addListener(new SupportAnimator.AnimatorListener() {
                        @Override
                        public void onAnimationStart() {
                            animator.start();
                        }

                        @Override
                        public void onAnimationEnd() {

                        }

                        @Override
                        public void onAnimationCancel() {

                        }

                        @Override
                        public void onAnimationRepeat() {

                        }
                    });
                }

                mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
                mAnimator.setDuration(600);
                mAnimator.start();
            }
        });
    }

    static float r(int a, int b) {
        return (float) Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
    }
}
